package com.uinnova.product.eam.service.impl;

import com.binary.core.util.BinaryUtils;
import com.uinnova.product.eam.dao.DiagramThumbnailDao;
import com.uinnova.product.eam.model.es.DiagramThumbnail;
import com.uinnova.product.eam.service.DiagramThumbnailService;
import com.uinnova.product.eam.vo.DiagramThumbnailSaveReq;
import com.uinnova.project.service.eam.ESDiagramSvc;
import com.uino.dao.util.ESUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Log4j2
public class DiagramThumbnailServiceImpl implements DiagramThumbnailService {

    @Autowired
    private DiagramThumbnailDao diagramThumbnailDao;
    @Autowired
    private ESDiagramSvc esDiagramSvc;
    @Value("${http.resource.space}")
    private String httpResourceUrl;

    @Override
    public List<DiagramThumbnail> saveDiagramThumbnail(List<DiagramThumbnailSaveReq> reqs) {
        List<DiagramThumbnail> diagramThumbnails = new ArrayList<>();
        String basePath = "/tmpDir";
        for (DiagramThumbnailSaveReq req : reqs) {
            try {
                String thumbnail = req.getContent();
                String diagramId = req.getDiagramId();
                String sheetId = req.getSheetId();
                BinaryUtils.checkEmpty(thumbnail, "缩略图信息串");
                BinaryUtils.checkEmpty(diagramId, "视图id");
                BinaryUtils.checkEmpty(sheetId, "sheetId");
                String iconPath = basePath.concat("/").concat(diagramId).concat("/").concat(sheetId).concat(".png");
                iconPath = esDiagramSvc.saveOrUpdatePng(iconPath, thumbnail, false);
                DiagramThumbnail diagramThumbnail = new DiagramThumbnail();
                diagramThumbnail.setId(ESUtil.getUUID());
                diagramThumbnail.setCreateTime(ESUtil.getNumberDateTime());
                diagramThumbnail.setDiagramId(diagramId);
                diagramThumbnail.setSheetId(sheetId);
                diagramThumbnail.setPath(httpResourceUrl.concat(iconPath));
                diagramThumbnails.add(diagramThumbnail);
            } catch (Exception e) {
                log.error("处理缩略图失败", e);
            }
        }
        return diagramThumbnails;

        //if (!CollectionUtils.isEmpty(diagramThumbnails)) {
        //    BoolQueryBuilder query = QueryBuilders.boolQuery();
        //    query.must(QueryBuilders.termQuery("diagramId.keyword", reqs.get(0).getDiagramId()));
        //    diagramThumbnailDao.deleteByQuery(query, true);
        //    diagramThumbnailDao.saveOrUpdateBatch(diagramThumbnails);
        //}
    }

    private DiagramThumbnail queryByIcon(String icon, String diagramId, String sheetId){
        if (StringUtils.isBlank(icon)) {
            return null;
        }
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.mustNot(QueryBuilders.termQuery("diagramId.keyword", diagramId));
        query.mustNot(QueryBuilders.termQuery("sheetId.keyword", sheetId));
        query.must(QueryBuilders.termQuery("path.keyword", icon.replace(httpResourceUrl, "")));
        return diagramThumbnailDao.selectOne(query);
    }

    @Override
    @Deprecated
    public List<DiagramThumbnail> list(String diagramId) {
        if (StringUtils.isBlank(diagramId)) {
            return new ArrayList<>();
        }
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.must(QueryBuilders.termQuery("diagramId.keyword", diagramId));
        List<DiagramThumbnail> diagramThumbnails = diagramThumbnailDao.getListByQuery(query);
        diagramThumbnails.sort(Comparator.comparing(DiagramThumbnail::getDiagramId));
        diagramThumbnails.forEach(diagramThumbnail -> diagramThumbnail.setPath(httpResourceUrl.concat(diagramThumbnail.getPath())));
        return diagramThumbnails;
    }
}
